package com.xbh.boot_study.controller;

import com.xbh.boot_study.annotion.valid.ParamNoNullValid;
import com.xbh.boot_study.annotion.valid.delegate.ValidationList;
import com.xbh.boot_study.annotion.valid.group.Query;
import com.xbh.boot_study.annotion.valid.group.sequence.GroupSeq;
import com.xbh.boot_study.entity.UserRequestVO;
import com.xbh.boot_study.util.ResponseData;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/valid")
@Validated
public class ValidController {

    @PostMapping("/saveByCusValid")
    public ResponseData<String> saveByCusValid(
            @RequestBody @ParamNoNullValid(groupings = Query.class) UserRequestVO vo) {
        // 校验失败则会抛出MethodArgumentNotValidException
        return ResponseData.success("suc!");
    }

    @GetMapping("/get/{id}")
    public ResponseData<String> get(
            @PathVariable("id")
            @Min(message = "id异常!", value = 1L)
            @Max(message = "id异常!", value = 100L) Long id) {
        // 校验失败则会抛出ConstraintViolationException
        return ResponseData.success("suc!");
    }

    @GetMapping("/queryById")
    public ResponseData<String> queryById(
            @Length(min = 1, max = 5, message = "id异常!")
            @NotNull(message = "id不能为空!") String id) {
        // 校验失败则会抛出ConstraintViolationException
        return ResponseData.success("suc!");
    }

    @PostMapping("/query")
    public ResponseData<String> query(@Validated(Query.class) UserRequestVO vo) {
        // 校验失败则会抛出MethodArgumentNotValidException
        return ResponseData.success("suc!");
    }

    @PostMapping("/queryByGSVaild")
    public ResponseData<String> queryByGSVaild(@Validated(GroupSeq.class) UserRequestVO vo) {
        // 校验失败则会抛出MethodArgumentNotValidException
        return ResponseData.success("suc!");
    }

    @PostMapping("/save")
    public ResponseData<String> save(@RequestBody @Validated UserRequestVO vo) {
        // 校验失败则会抛出MethodArgumentNotValidException
        return ResponseData.success("suc!");
    }

    @PostMapping("/saveAll")
    public ResponseData<String> saveAll(@RequestBody @Validated ValidationList<UserRequestVO> vo) {
        // 校验失败则会抛出MethodArgumentNotValidException
        return ResponseData.success("suc!");
    }

}
